import service from "@/api/service";
export default {
  namespaced: true,
  state: {
    initialized: false,
    loadUri: '',
    settings:[],
    maxFileLength: 524288,
    fileTypes: {},
    group: '',
    path: '/',
    super: null,
    folder: {

    },
    folders: [],
    selectedFolder: null,
    selectedFile: null
  },
  mutations: {
    initialize(state) {
      if (state.initialized)
        return;
      let arr = [
        {
          name: ".7z",
          icon: "fa-file-zip-o",
          typeName: "压缩文件",
          color: "#0086bc"
        },
        {
          name: ".csv",
          icon: "fa-file-excel-o",
          typeName: "CSV文件",
          color: "#107c41"
        },
        {
          name: ".default",
          icon: "fa-file",
          typeName: "普通文件",
          color: "#0086bc"
        },
        {
          name: ".doc",
          icon: "fa-file-word-o",
          typeName: "Word文档",
          color: "#185abd"
        },
        {
          name: ".docx",
          icon: "fa-file-word-o",
          typeName: "Word文档",
          color: "#185abd"
        },
        {
          name: ".eml",
          icon: "fa-envelope-o",
          typeName: "Email文件",
          color: "#2b5797"
        },
        {
          name: ".exe",
          icon: "fa-cogs",
          typeName: "可执行程序",
          color: "#0086bc"
        },
        {
          name: ".gif",
          icon: "fa-file-image-o",
          typeName: "图形文件",
          color: "#0086bc"
        },
        {
          name: ".htm",
          icon: "fa-file-code-o",
          typeName: "HTML文档",
          color: "#0086bc"
        },
        {
          name: ".html",
          icon: "fa-file-zip-o",
          typeName: "HTML文档",
          color: "#0086bc"
        },
        {
          name: ".ico",
          icon: "fa-file-image-o",
          typeName: "图标文件",
          color: "#0086bc"
        },
        {
          name: ".jpeg",
          icon: "fa-file-image-o",
          typeName: "图形文件",
          color: "#0086bc"
        },
        {
          name: ".jpg",
          icon: "fa-file-image-o",
          typeName: "图形文件",
          color: "#0086bc"
        },
        {
          name: ".mp3",
          icon: "fa-file-audio-o",
          typeName: "音频文件",
          color: "#0086bc"
        },
        {
          name: ".mp4",
          icon: "fa-file-video-o",
          typeName: "视频文件",
          color: "#0086bc"
        },
        {
          name: ".mpg",
          icon: "fa-file-video-o",
          typeName: "视频文件",
          color: "#0086bc"
        },
        {
          name: ".msg",
          icon: "fa-envelope-o",
          typeName: "Email文件",
          color: "#2b5797"
        },
        {
          name: ".pdf",
          icon: "fa-file-pdf-o",
          typeName: "PDF文档",
          color: "#d00202"
        },
        {
          name: ".png",
          icon: "fa-file-image-o",
          typeName: "图形文件",
          color: "#0086bc"
        },
        {
          name: ".ppt",
          icon: "fa-file-powerpoint-o",
          typeName: "PowerPoint文档",
          color: "#c43e1c"
        },
        {
          name: ".pptx",
          icon: "fa-file-powerpoint-o",
          typeName: "PowerPoint文档",
          color: "#c43e1c"
        },
        {
          name: ".rar",
          icon: "fa-file-zip-o",
          typeName: "压缩文件",
          color: "#0086bc"
        },
        {
          name: ".rm",
          icon: "fa-file-video-o",
          typeName: "视频文件",
          color: "#0086bc"
        },
        {
          name: ".rmvb",
          icon: "fa-file-video-o",
          typeName: "视频文件",
          color: "#0086bc"
        },
        {
          name: ".txt",
          icon: "fa-file-text-o",
          typeName: "文本文件",
          color: "#0086bc"
        },
        {
          name: ".wav",
          icon: "fa-file-audio-o",
          typeName: "音频文件",
          color: "#0086bc"
        },
        {
          name: ".wma",
          icon: "fa-file-audio-o",
          typeName: "音频文件",
          color: "#0086bc"
        },
        {
          name: ".xls",
          icon: "fa-file-excel-o",
          typeName: "Excel文件",
          color: "#107c41"
        },
        {
          name: ".xlsx",
          icon: "fa-file-excel-o",
          typeName: "Excel文件",
          color: "#107c41"
        },
        {
          name: ".zip",
          icon: "fa-file-zip-o",
          typeName: "压缩文件",
          color: "#0086bc"
        }
      ];
      let fileTypes = {};
      arr.forEach(e => {
        fileTypes[e.name] = { "icon": e.icon, "typeName": e.typeName, "color": e.color };
      });
      state.fileTypes = fileTypes;
      state.initialized = true;
    },
    setConfig(state, config){
      state.settings = config.data;
    },
    setGroup(state, group) {
      state.group = group;
      state.loadUri = `${service.file.load}/${group}`;
    },
    setPath(state, path) {
      state.path = path;
    },
    setData(state, data) {
      state.super = data.super;
      state.folder = data.folder;
      state.folders = data.children;
    },
    selectFolder(state, folder) {
      state.selectedFolder = folder;
    },
    selectFile(state, file) {
      state.selectedFile = file;
    }
  },
  actions: {
    getConfig({ commit, state }){
      return service.fileConfig.getConfig().then(r => {
        if (r.success){
          let config = r.data;
          commit("setConfig", config);
          commit('setGroup', config.defaultGroup);
        }
        return r;
      });
    },
    query({ commit, state }, path) {
      return service.folder
        .query({ routeData: { group: state.group }, data: path })
        .then((response) => {
          if (response.success){
            commit("setData", response.data);
            commit("setPath", path);
          }
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    create({ commit, state, dispatch }, newName) {
      let dto = {
        folderName: newName,
        path: state.folder.path
      };
      return service.folder
        .create({ routeData: { group: state.group }, data: dto })
        .then((response) => {
          if (response.success) dispatch("query", state.folder.path);
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    rename({ commit, state, dispatch }, newName) {
      let dto = {
        newName: newName,
        path: state.selectedFolder.path
      };
      return service.folder
        .renameFolder({ routeData: { group: state.group }, data: dto })
        .then((response) => {
          if (response.success) dispatch("query", state.folder.path);
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    renameFile({ commit, state, dispatch}, newName){
      let dto = {
        newName: newName,
        fileId: state.selectedFile.fileId
      };
      return service.storage
        .rename({ routeData: { group: state.group }, data: dto })
        .then((response) => {
          if (response.success) dispatch("query", state.folder.path);
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    move({ commit, state, dispatch }, dto) {
      return service.folder
        .move({ routeData: { group: state.group }, data: dto })
        .then((response) => {
          if (response.success) dispatch("query", state.folder.path);
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    delete({ commit, state, dispatch }, path) {
      return service.folder
        .remove({ routeData: { group: state.group }, data: path })
        .then((response) => {
          if (response.success) dispatch("query", state.folder.path);
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    deleteFile({ commit, state, dispatch }, dto) {
      return service.storage
        .remove({ routeData: { group: state.group }, data: dto })
        .then((response) => {
          if (response.success) dispatch("query", state.folder.path);
          return response;
        })
        .catch((error) => {
          return error;
        });
    },
    download({ commit, state, dispatch }, dto){
      return service.file.download({ routeData: { group: state.group }, data: dto});
    }
  },
};
